/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.infra.run;

import edu.cmu.lti.sepia.infra.run.model.AnswerCandidate;
import edu.cmu.lti.sepia.infra.run.model.ExtractionResult;
import edu.cmu.lti.sepia.infra.run.model.ExtractionResultSet;
import edu.cmu.lti.sepia.util.FormatUtil;

public class ExtractionResultObjToXml {

  public static String serialize( ExtractionResultSet resultSet ) {
    StringBuilder sb = new StringBuilder();
    sb.append("<TOPIC_SET>\n");
    if (resultSet.getMetadata().size()>0) {
      sb.append("  <METADATA>");
      for ( String key : resultSet.getMetadata().keySet() ) {
        sb.append("  <"+key+">"+resultSet.getMetadata().get(key)+"</"+key+">");
      }
      sb.append("  </METADATA>");
    }
    
    for ( String topicId : resultSet.keySet() ) {
      ExtractionResult result = resultSet.get(topicId);
      sb.append("  <TOPIC ID=\""+topicId+"\">\n");
      sb.append("    <CCLQA_RESULT>\n");
      for ( int i=0; i<result.size(); i++ ) {
        AnswerCandidate a = result.get(i);
        sb.append("      <ANSWER_CANDIDATE RANK=\""+(i+1)+"\" ");
        sb.append("DOCID=\""+a.getDocid()+"\" ");
        sb.append("SCORE=\""+FormatUtil.nf3(a.getScore())+"\">");
        sb.append("<![CDATA["+ a.getText() +"]]>");
        sb.append("</ANSWER_CANDIDATE>\n");
      }
      sb.append("    </CCLQA_RESULT>\n");
      sb.append("  </TOPIC>\n");
    }
    sb.append("</TOPIC_SET>\n");
    
    return sb.toString();
  }
  
}
